package demo;

import hcsdk.HCNetSDK;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import org.jdesktop.application.Action;

import javax.swing.*;
import java.awt.event.ItemEvent;

/**
 * @author yangxiaofeng6
 */
public class UserCfgBox extends javax.swing.JDialog {
    public static HCNetSDK hCNetSDK = HCNetSDK.INSTANCE;
    public static AlarmJavaDemoView m_alarm;
    public HCNetSDK.NET_DVR_USER_V50 m_struUserCfg = new HCNetSDK.NET_DVR_USER_V50();

    private javax.swing.JButton UserCfgClose;
    private javax.swing.JPasswordField adminPasswordField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JButton userCfgGetButton;
    private javax.swing.JButton userCfgSetButton;
    private javax.swing.JTextField userNameTextField;
    private javax.swing.JPasswordField userPasswordField;
    private javax.swing.JComboBox userSelComboBox;
    private javax.swing.JPasswordField userVerifyPasswordField;

    /**
     * Creates new form UserCfgBox
     */
    public UserCfgBox(java.awt.Frame parent, AlarmJavaDemoView obj) {
        super(parent);
        initComponents();
        m_alarm = obj;
        Init();
    }

    public void Init() {
        IntByReference dwReturned = new IntByReference(0);
        NativeLong lChannel = new NativeLong(0);

        Pointer lpUserCfg = m_struUserCfg.getPointer();

        if (hCNetSDK.NET_DVR_GetDVRConfig(m_alarm.lUserID, HCNetSDK.NET_DVR_GET_USERCFG_V50, lChannel, lpUserCfg, m_struUserCfg.size(), dwReturned)) {
            JOptionPane.showMessageDialog(null, "NET_DVR_GET_USERCFG_V50 true");
        } else {
            int code = hCNetSDK.NET_DVR_GetLastError();
            JOptionPane.showMessageDialog(null, "NET_DVR_GET_USERCFG_V50 failed： " + code);
            return;
        }
        m_struUserCfg.read();

        for (int i = 0; i < HCNetSDK.MAX_USERNUM_V30; i++) {
            String userSel = "用户" + (i + 1);
            userSelComboBox.addItem(i);
        }
        userSelComboBox.setSelectedIndex(0);
        userNameTextField.setText(new String(m_struUserCfg.struUser[0].sUserName));
        userPasswordField.setText(new String(m_struUserCfg.struUser[0].sPassword));
        userVerifyPasswordField.setText(new String(m_struUserCfg.struUser[0].sPassword));
    }

    @Action
    public void UserCfgBoxClose() {
        dispose();
    }

    @Action
    public void userCfgSetButtonClick() {
        String pwd = new String(userPasswordField.getPassword());
        String pwd2 = new String(userVerifyPasswordField.getPassword());
        String adminPwd = new String(adminPasswordField.getPassword());
        if (!pwd.equals(pwd2)) {
            JOptionPane.showMessageDialog(null, "两次密码不相等");
        }

        int index = userSelComboBox.getSelectedIndex();

        for (int i = 0; i < HCNetSDK.NAME_LEN; i++) {
            m_struUserCfg.struUser[index].sUserName[i] = 0;
        }
        for (int i = 0; i < userNameTextField.getText().length(); i++) {
            m_struUserCfg.struUser[index].sUserName[i] = userNameTextField.getText().getBytes()[i];
        }

        for (int i = 0; i < HCNetSDK.PASSWD_LEN; i++) {
            m_struUserCfg.struUser[index].sPassword[i] = 0;
        }
        for (int i = 0; i < pwd.length(); i++) {
            m_struUserCfg.struUser[index].sPassword[i] = pwd.getBytes()[i];
        }

        for (int i = 0; i < HCNetSDK.PASSWD_LEN; i++) {
            m_struUserCfg.sloginPassword[i] = 0;
        }
        for (int i = 0; i < adminPwd.length(); i++) {
            m_struUserCfg.sloginPassword[i] = adminPwd.getBytes()[i];
        }

        NativeLong lChannel = new NativeLong(0);

        m_struUserCfg.write();
        Pointer lpUserCfg = m_struUserCfg.getPointer();

        if (hCNetSDK.NET_DVR_SetDVRConfig(m_alarm.lUserID, HCNetSDK.NET_DVR_SET_USERCFG_V50, lChannel, lpUserCfg, m_struUserCfg.size())) {
            JOptionPane.showMessageDialog(null, "NET_DVR_SET_USERCFG_V50 true");
        } else {
            int code = hCNetSDK.NET_DVR_GetLastError();
            JOptionPane.showMessageDialog(null, "NET_DVR_SET_USERCFG_V50 failed: " + code);
            return;
        }
    }

    @Action
    public void userCfgGetButtonClick() {
        Init();
    }

    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        userCfgSetButton = new javax.swing.JButton();
        UserCfgClose = new javax.swing.JButton();
        userCfgGetButton = new javax.swing.JButton();
        userSelComboBox = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        userVerifyPasswordField = new javax.swing.JPasswordField();
        jLabel2 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        adminPasswordField = new javax.swing.JPasswordField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        userNameTextField = new javax.swing.JTextField();
        userPasswordField = new javax.swing.JPasswordField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(AlarmJavaDemoApp.class).getContext().getActionMap(UserCfgBox.class, this);
        userCfgSetButton.setAction(actionMap.get("userCfgSetButtonClick")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(AlarmJavaDemoApp.class).getContext().getResourceMap(UserCfgBox.class);
        userCfgSetButton.setText(resourceMap.getString("userCfgSetButton.text")); // NOI18N
        userCfgSetButton.setName("userCfgSetButton"); // NOI18N

        UserCfgClose.setAction(actionMap.get("UserCfgBoxClose")); // NOI18N
        UserCfgClose.setText(resourceMap.getString("UserCfgClose.text")); // NOI18N
        UserCfgClose.setName("UserCfgClose"); // NOI18N

        userCfgGetButton.setAction(actionMap.get("userCfgGetButtonClick")); // NOI18N
        userCfgGetButton.setText(resourceMap.getString("userCfgGetButton.text")); // NOI18N
        userCfgGetButton.setName("userCfgGetButton"); // NOI18N

        userSelComboBox.setName("userSelComboBox"); // NOI18N
        userSelComboBox.addItemListener(new java.awt.event.ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent evt) {
                userSelComboBoxItemStateChanged(evt);
            }
        });

        jLabel1.setName("jLabel1"); // NOI18N

        userVerifyPasswordField.setName("userVerifyPasswordField"); // NOI18N

        jLabel2.setName("jLabel2"); // NOI18N

        jLabel5.setName("jLabel5"); // NOI18N

        adminPasswordField.setName("adminPasswordField"); // NOI18N

        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setName("jLabel4"); // NOI18N

        userNameTextField.setName("userNameTextField"); // NOI18N

        userPasswordField.setName("userPasswordField"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        jLabel9.setText(resourceMap.getString("jLabel9.text")); // NOI18N
        jLabel9.setName("jLabel9"); // NOI18N

        jLabel10.setText(resourceMap.getString("jLabel10.text")); // NOI18N
        jLabel10.setName("jLabel10"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGap(59, 59, 59).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING).addComponent(jLabel5).addComponent(jLabel4).addComponent(jLabel3).addComponent(jLabel2).addComponent(jLabel1)).addGap(48, 48, 48).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING).addComponent(jLabel10).addComponent(jLabel7).addComponent(jLabel8).addComponent(jLabel9).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(userCfgGetButton).addComponent(jLabel6))).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false).addGroup(layout.createSequentialGroup().addGap(45, 45, 45).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false).addComponent(adminPasswordField, 0, 0, Short.MAX_VALUE).addComponent(userVerifyPasswordField, 0, 0, Short.MAX_VALUE).addComponent(userPasswordField, 0, 0, Short.MAX_VALUE).addComponent(userNameTextField).addComponent(userSelComboBox, 0, 114, Short.MAX_VALUE))).addGroup(layout.createSequentialGroup().addGap(18, 18, 18).addComponent(userCfgSetButton).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(UserCfgClose))).addContainerGap(94, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGap(35, 35, 35).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jLabel10).addGroup(layout.createSequentialGroup().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addComponent(jLabel1).addGap(44, 44, 44).addComponent(jLabel2).addGap(37, 37, 37).addComponent(jLabel3).addGap(30, 30, 30).addComponent(jLabel4).addGap(30, 30, 30).addComponent(jLabel5)).addGroup(layout.createSequentialGroup().addComponent(userSelComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addGap(23, 23, 23).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(userNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(jLabel9)).addGap(37, 37, 37).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(userPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(jLabel8)).addGap(30, 30, 30).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(userVerifyPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(jLabel7)).addGap(30, 30, 30).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(adminPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(jLabel6)))).addGap(32, 32, 32).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(userCfgGetButton).addComponent(userCfgSetButton).addComponent(UserCfgClose)))).addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void userSelComboBoxItemStateChanged(ItemEvent evt) {//GEN-FIRST:event_userSelComboBoxItemStateChanged
        // TODO add your handling code here:
        if (evt.getStateChange() == ItemEvent.SELECTED) {
            int index = (Integer) evt.getItem();
            userNameTextField.setText(new String(m_struUserCfg.struUser[index].sUserName));
            userPasswordField.setText(new String(m_struUserCfg.struUser[index].sPassword));
            userVerifyPasswordField.setText(new String(m_struUserCfg.struUser[index].sPassword));
        }
    }//GEN-LAST:event_userSelComboBoxItemStateChanged
    // End of variables declaration//GEN-END:variables

}
